﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

namespace DevEx.Thanh.Class
{
    class CTonController
    {
        private QLVTDataContext objDataContext;
        public CTonController ()
        {
            objDataContext = new QLVTDataContext();
        }
        public CTonController (QLVTDataContext _obj)
        {
            objDataContext = _obj;
        }
        public ISingleResult<sp_T_GetVatTuTonResult > GetTonInKho(int idKho)
        {
            return objDataContext.sp_T_GetVatTuTon(idKho);
        }
        public IQueryable GetTonKho2(int idKho)
        {
            try                
            {
                QLVTDataContext dc = new QLVTDataContext();

                return (from vt in dc.tblVatTus
                        from t in dc.tblTons
                        where vt.ID_VatTu == t.ID_VatTu && t.ID_Kho == idKho
                        orderby vt.VatTu
                        select new { t.ID_MatHang, vt.VatTu, vt.MaKyHieu, SoTheKho = t.SoTheKho + "-" + t.Code.ToString()  , t.TangKeKhoang })
            ;
            }
            catch (Exception){

                return null;
                //throw;
            }
        }
        public ISingleResult<sp_Thanh_TTNhapXuatMatHangResult> GetTTNhapXuat(int idMatHang)
        {
            return objDataContext.sp_Thanh_TTNhapXuatMatHang(idMatHang);
        }
        public IQueryable GetThongTinNhapXuat(int idMatHang)
        {
            //không dùng store procedure
            var query = from ct in objDataContext.tblSLNXes
                        from p in objDataContext.tblDPNXes 
                        where ct.ID_DPNX == p.ID_DPNX && ct.ID_MatHang == idMatHang 
                        select new
                                   {
                                       NgayPhieu = p.NgayPhieu,
                                       PhieuNhap = p.NX == "N"? p.SoPhieu.ToString() : "",
                                       PhieuXuat = p.NX =="X"?p.SoPhieu.ToString() :"",
                                       SLNhap =p.NX =="N"?ct.SLTH:0,
                                       SLXuat =p.NX =="X"?ct.SLTH :0,
                                       ct.GhiChu 
                                   };
            return query;
        }
        public ISingleResult <sp_T_KiemTraSoLieuResult > KiemTraSoLieu(int idMaKho)
        {
            try
            {
                return objDataContext.sp_T_KiemTraSoLieu(idMaKho);
            }
            catch (Exception)
            {

                return null;
            }
            
        }
        public ISingleResult<sp_T_KiemTraSoLieu_AllResult > KiemTraSoLieuAll()
        {
            try
            {
                return objDataContext.sp_T_KiemTraSoLieu_All();
            }
            catch (Exception)
            {

                return null;
            }

        }
        public tblTon SelectRecordById(int idTon)
        {
            try
            {
                return (from t in objDataContext.tblTons
                        where t.ID_MatHang == idTon
                        select t).SingleOrDefault();
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
